package com.example.studentmanagmentsystem;

import android.content.DialogInterface;
import android.graphics.Path;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import org.litepal.Operator;

import java.util.List;

public class ScoreAdapter extends RecyclerView.Adapter<ScoreAdapter.ViewHolder> {
    private OnItemClickListener mOnItemClickListener;
    private OnDeleteClickListener onDeleteClickListener;

    public OnDeleteClickListener getOnDeleteClickListener() {
        return onDeleteClickListener;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    public OnItemClickListener getmOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    private List<Score> scores;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView studentId;
        TextView studentName;
        TextView courseName;
        TextView courseScore;
        Button updateScore;
        Button deleteScore;

        public ViewHolder(View view) {
            super(view);
            studentId = (TextView) view.findViewById(R.id.student_id1);
            studentName = (TextView) view.findViewById(R.id.student_name1);
            courseName = (TextView) view.findViewById(R.id.course_name1);
            courseScore = (TextView) view.findViewById(R.id.course_score1);
            updateScore = (Button) view.findViewById(R.id.update_score1);
            deleteScore = (Button) view.findViewById(R.id.delete_score1);

        }
    }

    public ScoreAdapter(List<Score> scores) {
        this.scores = scores;
    }

    @Override
    public ScoreAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.score_list, parent, false);
        final ScoreAdapter.ViewHolder holder = new ScoreAdapter.ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ScoreAdapter.ViewHolder holder, int position) {
        Score score = scores.get(position);

        List<Student> students = Operator.findAll(Student.class);//查询出所有学生
        List<Course> courses = Operator.findAll(Course.class);//查询出所有课程

        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId() == score.getStudentId()) {
                holder.studentName.setText(students.get(i).getName());
            }
        }
        for (int i = 0; i < courses.size(); i++) {
            if (courses.get(i).getId() == score.getCourseId()) {
                holder.courseName.setText(courses.get(i).getName());
            }
        }

        holder.studentId.setText(score.getStudentId() + "");//赋值
        holder.courseScore.setText(score.getScore() + "");

        holder.deleteScore.setOnClickListener(new View.OnClickListener() {//删除学生成绩数据
            @Override
            public void onClick(View v) {
                int position_delete = holder.getLayoutPosition();
                //在MainActivity中实现
                onDeleteClickListener.onDeleteClick(holder.itemView, position_delete);
                //mOnItemClickListener.onItemClick(holder.itemView, position_delete);
            }
        });


        holder.updateScore.setOnClickListener(new View.OnClickListener() {//在Adapter类中修改学生成绩
            @Override
            public void onClick(View v) {
                //获取当前点击的item位置信息
                int position_update = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder.itemView,position_update);

            }
        });

    }

    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener{
        public void clickListener(View v);
    }

    @Override
    public int getItemCount() {
        return scores.size();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnDeleteClickListener{
        void onDeleteClick(View view, int position);
    }
}
